<template>
    <el-form :model="rider" label-position="right" label-width="auto">
        <el-form-item label="骑手姓名" prop="name">
            <el-input v-model="rider.name" placeholder="骑手姓名" disabled clearable/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
            <el-input v-model="rider.phone" placeholder="手机号" clearable/>
        </el-form-item>
        <el-form-item label="认证状态" prop="state">
            <el-select v-model="rider.state" placeholder="请选择" clearable>
                <el-option label="已认证" value="已认证"/>
                <el-option label="待认证" value="待认证"/>
            </el-select>
        </el-form-item>
        <el-form-item label="工作收入" prop="gzsr">
            <el-input v-model="rider.gzsr" placeholder="工作收入" clearable/>
        </el-form-item>
        <el-form-item label="评分" prop="mark">
            <el-input v-model="rider.mark" placeholder="评分" clearable/>
        </el-form-item>
    </el-form>
    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "closeDialog",	// 关闭 dialog
    "flushPage"		  // 刷新页面
]);

const rider = ref({
    id: '',
    name: '',
    pwd: '',
    phone: '',
    state: '',
    gzsr: 0.0,
    mark: 5.0
})

// 将父组件传递来的的参数，保存到 rider 中
const handleUpdateData = (obj) => {
    rider.value = obj;
}

// 暴露给父组件使用的方法
defineExpose({
    handleUpdateData
})

// 取消
const doCancel = () => {
    emits("closeDialog");
}

// 更新
const doSave = async() => {
    const {data} = await axios.put("/api/rider",rider.value);
    if (data > 0) {
        emits("flushPage");			// 触发事件：刷新页面
        emits("closeDialog");			// 触发事件：关闭 更新 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>